package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Route;
import com.ws.lv.bean.Type;
import com.ws.lv.service.RouteService;
import com.ws.lv.service.TypeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/type")
public class TypeController {

    @Autowired
    TypeService typeService;
    @Autowired
    RouteService routeService;

    //去增加页面
    @RequestMapping("/toAddType")
    public String toAddType() {
        return "adminpage/type/add";
    }
    //多条件查询类别
    @RequestMapping("/selectTypes")
    public String selectTypes(Model model, String type_name, Integer type_state, String type_order ,Integer currentPage) {
        System.out.println("多条件查询类别");

        PageHelper pageHelper = new PageHelper();//分页帮助类
        if (currentPage == null){
            pageHelper.startPage(1, 5);
        }else {
            pageHelper.startPage(currentPage, 5);
        }
        List<Type> types = typeService.selectTypes(type_name, type_state, type_order);
        PageInfo pageInfo = new PageInfo(types);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("currentPage", currentPage);
        model.addAttribute("type_name", type_name);
        model.addAttribute("type_state", type_state);
        model.addAttribute("type_order", type_order);
        return "adminpage/type/list";
    }

    //增加类别
    @RequestMapping("/insertType")
    public String insertType(Type type) {
        System.out.println("增加类别");
        System.out.println(type);
        int i = typeService.insertType(type);
        return "redirect:/type/selectTypes";
    }

    //修改类别显示
    @RequestMapping("/updateTypeDisplay")
    public String updateTypeDisplay(int type_id) {
        System.out.println("修改类别显示");
        int i = typeService.updateTypeDisplay(type_id);
        return "forward:/type/selectTypes";
    }

    //修改类别隐藏
    @RequestMapping("/updateTypeHide")
    public String updateTypeHide(int type_id) {
        System.out.println("修改类别隐藏");
        int i = typeService.updateTypeHide(type_id);
        return "forward:/type/selectTypes";
    }

    //删除多个类别
    @RequestMapping("/deleteTypes")
    public String deleteTypes(String type_ids) {
        System.out.println("删除多个类别");
        ArrayList<Integer> integers = new ArrayList<>();
        boolean flag = true;
        if (type_ids != null && type_ids.trim().length() > 0){
            String[] split = type_ids.split(",");
            for (String s : split) {
                integers.add(Integer.parseInt(s));
            }
            for (Integer integer : integers){
                List<Route> routes = routeService.selectRoutesByType(integer);
                if (routes.size() > 0){
                    System.out.println(routes);
                    flag = false;
                }
            }
        }
        System.out.println(flag+"--------------");
        if (flag){
            int i = typeService.deleteTypes(integers);
        }
        return "redirect:/type/selectTypes";
    }

    //修改类别
    @RequestMapping("/updateType")
    public String updateType(Type type) {
        System.out.println("修改类别");
        System.out.println(type);
        int i = typeService.updateType(type);
        return "redirect:/type/selectTypes";
    }
    //去修改页面
    @RequestMapping("/toUpdateType")
    public String toUpdateType(int type_id, Model model) {
        Type type = typeService.selectTypeById(type_id);
        model.addAttribute("type",type);
        return "adminpage/type/update";
    }


}
